/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pin.db.devel;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import pin.db.dbcp.Dbcp;

/**
 *
 * @author didyhu
 */
public class DbTest {

    public static void main(String[] args) throws Exception {
        Connection conn = Dbcp.createDataSource("dbcp1.properties").getConnection();
        conn.setAutoCommit(true);
        final int innerLoop=1000;
        final int outerLoop=1;
        /*
        {
            Date start = new Date();
            for (int i = 0; i < outerLoop; i++) {
                testCallableStatement(conn, innerLoop);
            }
            Date end = new Date();
            System.out.println(String.format("%,dms", end.getTime() - start.getTime()));
        }
         */
        {
            Date start = new Date();
            for (int i = 0; i < outerLoop; i++) {
                testStatement(conn, innerLoop);
            }
            Date end = new Date();
            System.out.println(String.format("%,dms", end.getTime() - start.getTime()));
        }
        {
            Date start = new Date();
            for (int i = 0; i < outerLoop; i++) {
                testPreparedStatement(conn, innerLoop);
            }
            Date end = new Date();
            System.out.println(String.format("%,dms", end.getTime() - start.getTime()));
        }
        conn.close();

    }

    public static void testCallableStatement(Connection conn, int times) throws SQLException {
        CallableStatement cstmt = conn.prepareCall("{CALL pin.test(?)}");
        for (int i = 0; i < times; i++) {
            cstmt.setString("str", "world");
            cstmt.execute();
        }

    }

    public static void testStatement(Connection conn, int times) throws SQLException {
        Statement stmt = conn.createStatement();
        for (int i = 0; i < times; i++) {
            stmt.execute("INSERT `pin`.`test2` SET `value`='world';");
        }

    }

    public static void testPreparedStatement(Connection conn, int times) throws SQLException {
        PreparedStatement pstmt = conn.prepareStatement("INSERT `pin`.`test2` SET `value`=?;");
        for (int i = 0; i < times; i++) {
            pstmt.setString(1, "world");
            pstmt.execute();
        }
    }
}
